package com.yonyougov.devops.cruxcisupport.k8s;

import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.UrlResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.rmi.server.ExportException;


@RestController
@RequestMapping("/api/v2")
public class Test {

    @Autowired
    private ApplicationContext applicationContext;

    @RequestMapping("/k8s/test")
    @ResponseBody
    public String api10() throws IOException {
        String adminConfData = "apiVersion: v1\n" +
                "clusters:\n" +
                "- cluster:\n" +
                "    insecure-skip-tls-verify: true\n" +
                "    server: https://rancher.yonyougov.top/k8s/clusters/c-96l9c\n" +
                "  name: govdev\n" +
                "contexts:\n" +
                "- context:\n" +
                "    cluster: govdev\n" +
                "    namespace: dev81\n" +
                "    user: govdev\n" +
                "  name: dev81\n" +
                "current-context: dev81\n" +
                "kind: Config\n" +
                "users:\n" +
                "- name: govdev\n" +
                "  user:\n" +
                "    token: kubeconfig-user-k2phb.c-96l9c:57js4skfflkchvr55mtfpc6cjh6nfqcbbrbnrtmb7ljrldzpdgn2mf";
        Config config = Config.fromKubeconfig(adminConfData);
        try (final KubernetesClient k8s = new DefaultKubernetesClient(config)) {

            UrlResource deploymentResource = new UrlResource("https://dev.yonyougov.top/config/test-dev81test.yaml");
            UrlResource serviceResource = new UrlResource("https://dev.yonyougov.top/config/test-svc-dev81test.yaml");
            Deployment deploy  = null;
            Service service = null;

            try {
                deploy = k8s.apps().deployments().load(deploymentResource.getInputStream()).get();
            } catch (IOException e) {
                throw new ExportException("https://dev.yonyougov.top/config/test-svc-dev81test.yaml 文件异常");
            }
            try {
                service = k8s.services().load(serviceResource.getInputStream()).get();
            } catch (IOException e) {
                throw new ExportException("https://dev.yonyougov.top/config/test-svc-dev81test.yaml 文件异常");
            }
            k8s.apps().deployments().inNamespace("dev81").createOrReplace(deploy);
            k8s.services().inNamespace("dev81").createOrReplace(service);
            // Load YAML manifest into Java object


            ;
            //String yamlStr = StreamUtils.copyToString(resource161.getInputStream(), StandardCharsets.UTF_8);
            //deploy = k8s.apps().deployments().load(new ByteArrayInputStream(yaml.getBytes())).get();
            //for (String yaml : Arrays.asList(yamlStr.split("---"))) {
            //    if(yaml.contains("kind: Deployment")){
            //        deploy = k8s.apps().deployments().load(new ByteArrayInputStream(yaml.getBytes())).get();
            //        k8s.apps().deployments().inNamespace("dev81").createOrReplace(deploy);
            //    }
            //    if(yaml.contains("kind: Service")){
            //        service = k8s.services().load(new ByteArrayInputStream(yaml.getBytes())).get();
            //        k8s.services().inNamespace("dev81").createOrReplace(service);
            //    }
            //}
        }

        return "success";
    }

}
